<?php
namespace app\common\model;

use think\Model;

class GoodsDoctorComment extends BaseModel
{
    protected $name = 'goods_doctor_comment';
    protected $pk = 'comment_id';
    // protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $autoWriteTimestamp = false;
    protected $type = [
        'images' => 'json',
        'tags'   => 'json',
        'create_time' => 'string',
    ];
    /**
     * undocumented function summary
     *
     * Undocumented function long description
     *
     * @param Type $var Description
     * @return type
     * @throws conditon
     **/
public function getList($params = [])
{
    $page   = isset($params['page'])  ? (int)$params['page']  : 1;
    $limit  = isset($params['limit']) ? (int)$params['limit'] : 15;
    $search = isset($params['search'])? trim($params['search']) : '';

    $query = $this->alias('c')
        ->field([
            'c.*',
            'g.goods_name',
            'u.nickName AS user_name',
            'uj.name  AS jiuzhen_name',
            'uj.phone AS jiuzhen_phone',
        ])
        // 商品
        ->join(['yoshop_goods' => 'g'], 'c.goods_id = g.goods_id', 'LEFT')
        // 订单 -> 用户（通过订单拿 user_id）
        ->join(['yoshop_order' => 'o'], 'c.order_id = o.order_id', 'LEFT')
        ->join(['yoshop_user'  => 'u'], 'o.user_id = u.user_id',  'LEFT')
        // 订单商品（用 订单ID + 商品ID 关联到该订单里的该商品行）
        ->join(['yoshop_order_goods' => 'og'], 'og.order_id = c.order_id AND og.goods_id = c.goods_id', 'LEFT')
        // 就诊人（从订单商品里的 jiuzhen_id 取）
        ->join(['yoshop_user_jiuzhen' => 'uj'], 'uj.jiuzhen_id = og.jiuzhen_id AND uj.is_del = 0', 'LEFT')
        ->order('c.create_time DESC');

    if ($search !== '') {
        $query->where(function ($q) use ($search) {
            $digits = preg_replace('/\D+/', '', $search);
            $like   = "%{$search}%";
            $q->where('g.goods_name', 'like', $like)
              ->whereOr('u.nickName',  'like', $like)
              ->whereOr('uj.name',     'like', $like);
            if ($digits !== '') {
                $q->whereOr('uj.phone', 'like', "%{$digits}%");
            }
        });
    }

    return $query->paginate([
        'list_rows' => $limit,
        'page'      => $page,
        'query'     => $params,
    ]);
}
    public function getIsShowTextAttr($value, $data)
    {
        return $data['is_show'] ? '显示' : '隐藏';
    }
}